package com.example.liverock.androidalltest;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class ImageActivity extends Activity {

    /**
     * 全局变量
     */
    ImageSwitcher imageSwitcher = null;
    TypedArray galleryImage = null;
    int[] imageId = null;             // 图片数组
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);

        // 获得数组的图片
        galleryImage = getResources().obtainTypedArray(R.array.galleryImage);
        imageId = new int[galleryImage.length()];
        // 获得数组图片对应的Id
        for (int i = 0; i < galleryImage.length(); i++)
        {
            imageId[i] = galleryImage.getResourceId(i,0);
        }

        imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);
        Gallery gallery = (Gallery)findViewById(R.id.gallery);
        // 设置动画效果
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            /**
             * 创建用于显示图片的ImageView
             */
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(ImageActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,  ViewGroup.LayoutParams.WRAP_CONTENT
                ));
                return imageView;
            }
        });


        /**
         * Gallery 的图片适配器
         */
        BaseAdapter galleryAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return galleryImage.length();
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                ImageView imageView;
                if (convertView == null)
                {
                    imageView = new ImageView(ImageActivity.this);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY); // 设置缩放方式
                    imageView.setLayoutParams(new Gallery.LayoutParams(180, 135));
                    TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
                    imageView.setBackgroundResource(typedArray.getResourceId(
                            R.styleable.Gallery_android_galleryItemBackground,
                            0));
                    imageView.setPadding(5, 0, 5, 0); // 设置ImageView的内边距
                }
                else {
                    imageView = (ImageView) convertView;
                }
                imageView.setImageResource(imageId[position]); // 为ImageView设置要显示的图片
                return imageView;
            }
        };
        gallery.setAdapter(galleryAdapter); // 将适配器与Gallery关联
        /*********************************************************************************/
        gallery.setSelection(galleryImage.length() / 2); // 让中间的图片选中
        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                imageSwitcher.setImageResource(imageId[position]);// 显示选中的图片
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }

        });
    }
}
